// Logo 生成器 - 每次生成完全随机的 logo
(function() {
  'use strict';

  // 生成随机颜色
  function generateColor() {
    const hue = Math.floor(Math.random() * 360);
    const saturation = 50 + Math.floor(Math.random() * 40); // 50-90%
    const lightness = 35 + Math.floor(Math.random() * 30); // 35-65%
    return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
  }

  // 生成高对比度的颜色（用于图标）
  function generateContrastColor() {
    const hue = Math.floor(Math.random() * 360);
    const saturation = 60 + Math.floor(Math.random() * 30);
    const lightness = 40 + Math.floor(Math.random() * 20);
    return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
  }

  // 生成随机渐变方向
  function getGradientDirection(ctx, size) {
    const directions = [
      [0, 0, size, size], // 对角线
      [0, 0, size, 0],   // 水平
      [0, 0, 0, size],    // 垂直
      [size, 0, 0, size], // 另一条对角线
      [size/2, 0, size/2, size], // 垂直中心
      [0, size/2, size, size/2] // 水平中心
    ];
    const dir = directions[Math.floor(Math.random() * directions.length)];
    return ctx.createLinearGradient(dir[0], dir[1], dir[2], dir[3]);
  }

  // 生成径向渐变
  function getRadialGradient(ctx, size) {
    const cx = size / 2 + (Math.random() - 0.5) * size * 0.3;
    const cy = size / 2 + (Math.random() - 0.5) * size * 0.3;
    const r = size * (0.5 + Math.random() * 0.5);
    return ctx.createRadialGradient(cx, cy, 0, cx, cy, r);
  }

  // 绘制音符样式
  function drawNote(ctx, centerX, centerY, size) {
    const radius = size / 4;
    const noteX = centerX + (Math.random() - 0.5) * size * 0.2;
    const noteY = centerY - radius * 0.5;
    
    // 音符头部
    const headSize = radius * (0.15 + Math.random() * 0.15);
    ctx.beginPath();
    ctx.arc(noteX, noteY - radius * 0.8, headSize, 0, Math.PI * 2);
    ctx.fill();

    // 音符杆
    const stemLength = radius * (0.8 + Math.random() * 0.4);
    ctx.beginPath();
    ctx.moveTo(noteX + headSize * 0.5, noteY - radius * 0.8);
    ctx.lineTo(noteX + headSize * 0.5, noteY - radius * 0.8 - stemLength);
    ctx.stroke();

    // 音波圆
    const waveRadius = radius * (0.4 + Math.random() * 0.3);
    ctx.beginPath();
    ctx.arc(centerX, centerY, waveRadius, 0, Math.PI * 2);
    ctx.stroke();
  }

  // 绘制音乐符号样式
  function drawMusicSymbol(ctx, centerX, centerY, size) {
    const radius = size / 4;
    
    // 多个音符
    const noteCount = 1 + Math.floor(Math.random() * 3);
    for (let i = 0; i < noteCount; i++) {
      const angle = (Math.PI * 2 / noteCount) * i + Math.random() * 0.5;
      const x = centerX + Math.cos(angle) * radius * 0.6;
      const y = centerY + Math.sin(angle) * radius * 0.6;
      
      ctx.beginPath();
      ctx.arc(x, y, radius * (0.15 + Math.random() * 0.1), 0, Math.PI * 2);
      ctx.fill();
    }

    // 中心圆
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius * (0.3 + Math.random() * 0.2), 0, Math.PI * 2);
    ctx.stroke();
  }

  // 绘制波形样式
  function drawWaveform(ctx, centerX, centerY, size) {
    const radius = size / 4;
    const waveCount = 3 + Math.floor(Math.random() * 4);
    
    for (let i = 0; i < waveCount; i++) {
      const waveRadius = radius * (0.3 + (i / waveCount) * 0.7);
      const startAngle = Math.random() * Math.PI * 2;
      const endAngle = startAngle + Math.PI * (1.5 + Math.random());
      
      ctx.beginPath();
      ctx.arc(centerX, centerY, waveRadius, startAngle, endAngle);
      ctx.stroke();
    }
  }

  // 绘制几何图形样式
  function drawGeometric(ctx, centerX, centerY, size) {
    const radius = size / 4;
    const shapeType = Math.floor(Math.random() * 3);
    
    if (shapeType === 0) {
      // 六边形
      ctx.beginPath();
      for (let i = 0; i < 6; i++) {
        const angle = (Math.PI / 3) * i;
        const x = centerX + Math.cos(angle) * radius;
        const y = centerY + Math.sin(angle) * radius;
        if (i === 0) ctx.moveTo(x, y);
        else ctx.lineTo(x, y);
      }
      ctx.closePath();
      ctx.stroke();
    } else if (shapeType === 1) {
      // 星形
      const points = 5 + Math.floor(Math.random() * 3);
      ctx.beginPath();
      for (let i = 0; i < points * 2; i++) {
        const angle = (Math.PI / points) * i;
        const r = i % 2 === 0 ? radius : radius * 0.5;
        const x = centerX + Math.cos(angle) * r;
        const y = centerY + Math.sin(angle) * r;
        if (i === 0) ctx.moveTo(x, y);
        else ctx.lineTo(x, y);
      }
      ctx.closePath();
      ctx.stroke();
    } else {
      // 多个圆
      const circleCount = 3 + Math.floor(Math.random() * 4);
      for (let i = 0; i < circleCount; i++) {
        const angle = (Math.PI * 2 / circleCount) * i;
        const x = centerX + Math.cos(angle) * radius * 0.6;
        const y = centerY + Math.sin(angle) * radius * 0.6;
        ctx.beginPath();
        ctx.arc(x, y, radius * (0.2 + Math.random() * 0.2), 0, Math.PI * 2);
        ctx.stroke();
      }
    }
  }

  // 生成 logo
  function generateLogo(size = 128) {
    const canvas = document.createElement('canvas');
    canvas.width = size;
    canvas.height = size;
    const ctx = canvas.getContext('2d');

    // 随机选择背景类型
    const bgType = Math.random();
    let gradient;
    
    if (bgType < 0.6) {
      // 线性渐变
      gradient = getGradientDirection(ctx, size);
    } else {
      // 径向渐变
      gradient = getRadialGradient(ctx, size);
    }
    
    const color1 = generateContrastColor();
    const color2 = generateContrastColor();
    gradient.addColorStop(0, color1);
    gradient.addColorStop(1, color2);
    
    // 添加中间色（可选）
    if (Math.random() > 0.5) {
      gradient.addColorStop(0.5, generateContrastColor());
    }
    
    ctx.fillStyle = gradient;
    ctx.fillRect(0, 0, size, size);

    // 随机选择图标样式
    const iconStyle = Math.random();
    const centerX = size / 2;
    const centerY = size / 2;

    // 设置图标颜色（白色或随机颜色）
    if (Math.random() > 0.3) {
      ctx.strokeStyle = '#ffffff';
      ctx.fillStyle = '#ffffff';
    } else {
      const iconColor = generateContrastColor();
      ctx.strokeStyle = iconColor;
      ctx.fillStyle = iconColor;
    }

    ctx.lineWidth = size / (15 + Math.random() * 10);
    ctx.lineCap = 'round';
    ctx.lineJoin = 'round';

    // 根据随机数选择不同的图标样式
    if (iconStyle < 0.3) {
      drawNote(ctx, centerX, centerY, size);
    } else if (iconStyle < 0.6) {
      drawMusicSymbol(ctx, centerX, centerY, size);
    } else if (iconStyle < 0.8) {
      drawWaveform(ctx, centerX, centerY, size);
    } else {
      drawGeometric(ctx, centerX, centerY, size);
    }

    // 随机添加装饰元素
    if (Math.random() > 0.6) {
      ctx.globalAlpha = 0.3;
      const decorCount = 2 + Math.floor(Math.random() * 3);
      for (let i = 0; i < decorCount; i++) {
        const x = Math.random() * size;
        const y = Math.random() * size;
        const r = size * (0.05 + Math.random() * 0.1);
        ctx.beginPath();
        ctx.arc(x, y, r, 0, Math.PI * 2);
        ctx.fill();
      }
      ctx.globalAlpha = 1;
    }

    return canvas.toDataURL('image/png');
  }

  // 导出函数
  if (typeof module !== 'undefined' && module.exports) {
    module.exports = { generateLogo };
  } else {
    window.logoGenerator = { generateLogo };
  }
})();

